﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel.Design;
using SedionFormDesigner.Infrastructure;

namespace SedionFormDesigner.Designer
{
    public class SedionDocumentWindow : Control
    {
        private IDesignerHost designerHost;
        // 最终显示的视图
        private Control designerView;

        public SedionDocumentWindow(IDesignerHost designerHost)
        {
            this.designerHost = designerHost;
        }

        /// <summary>
        ///  读取/设置是否可见
        /// </summary>
        public Boolean Visibility
        {
            get
            {
                return (designerView == null ? false : designerView.Visible);
            }
            set
            {
                if (designerView != null)
                    designerView.Visible = value;
            }
        }

        /// <summary>
        ///  释放资源
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                SetDesigner(null);

                designerHost = null;
            }

            base.Dispose(disposing);
        }

        /// <summary>
        ///  设置designerView Focus()
        /// </summary>
        protected override void OnGotFocus(EventArgs e)
        {
            if (designerView != null)
                designerView.Focus();
            else
                base.OnGotFocus(e);
        }

        /// <summary>
        ///  获得值标志是否聚焦
        /// </summary>
        public override bool Focused
        {
            get
            {
                if (designerView != null)
                    return designerView.Focused;
                else
                    return base.Focused;
            }
        }

        /// <summary>
        ///  绘制带有错误的窗体
        /// </summary>
        public void ReportErrors(ICollection errors)
        {
            if (errors.Count > 0)
            {
                ListBox list = new ListBox();
                foreach (Object error in errors)
                {
                    list.Items.Add(error);
                }

                list.Dock = DockStyle.Fill;
                list.Height = 200;
                Controls.Add(list);
            }
        }

        /// <summary>
        ///  设置底层的视图
        /// </summary>
        public void SetDesigner(IRootDesigner document)
        {
            if (designerView != null)
            {
                Controls.Clear();
                designerView.Dispose();
                designerView = null;
            }
            addTab(document);
        }

        private void addTab(IRootDesigner document)
        {
            if (document != null)
            {
                // 如果视图不存在那么创建一个
                ViewTechnology[] technologies = document.SupportedTechnologies;
                Boolean supportedTechnology = false;

                // 循环搜索指定的视图
                foreach (ViewTechnology tech in technologies)
                {
                    switch (tech)
                    {
                        case ViewTechnology.Default:
                            designerView = (Control)document.GetView(ViewTechnology.Default);
                            changeSize(designerView);
                            designerView.Dock = DockStyle.Fill;
                            Controls.Add(designerView);
                            supportedTechnology = true;
                            break;
                    }

                    if (supportedTechnology)
                        break;
                }

                // 没有搜索到报错
                if (!supportedTechnology)
                    throw new Exception(String.Concat("Unsupported Technology ", designerHost.GetType().FullName));
            }
        }

        #region Form视图的外设

        private void changeSize(Control ctrl)
        {
            if (ctrl.GetType() == typeof(System.Windows.Forms.Form))
            {
                ctrl.Size = new System.Drawing.Size(595, 784);
                ctrl.SizeChanged += new EventHandler(ctrl_SizeChanged);
            }

            if (ctrl.Controls.Count > 0)
            {
                foreach (Control item in ctrl.Controls)
                {
                    changeSize(item);
                }
            }
        }

        private void ctrl_SizeChanged(object sender, EventArgs e)
        {
            ((Control)sender).Size = new System.Drawing.Size(595, 784);
        }

        #endregion
    }
}
